<?php

include_once(APP_URL.'protected/lib/private/Check.php');

class ProjectController extends BaseController {

    public function init() {
        if(!$this->session()) {
            $this->redirect(URL_USER_LOGIN);
            exit;
        }
        $this->layout = '//layouts/layout_developer';
    }

    public function actionIndex() {
        // http://192.168.3.69/project/index

        $this->render('index');
    }

    public function actionList() {
        // http://192.168.3.69/project/list

        $userId = Yii::app()->session['userId'];
        $TblProject = new TblProject();
        $condArr = array(
            'select' => array('*'),
            'order' => 'id DESC',
            'condition' => 'user_id=:userId',
            'params' => array('userId' => $userId)
        );
        $result = $TblProject->selectRecordAll($condArr);

        $this->render('list', array('listArr'=>$result));
    }

    public function actionAdd() {
        // http://192.168.3.69/project/add

        $name = Check::checkPostParam(-1, 'name', false);

        $userId = Yii::app()->session['userId'];
        $time = time();
        $bucket = BUCKET_DEMO;

        $TblProject = new TblProject();
        $contArr = array(
            'user_id' => $userId,
            'name' => $name,
            'bucket' => $bucket,
            'time' => $time,
        );
        if(!$TblProject->addRecord('project', $contArr)) {
            echo Resp::retFail(-2, 'server error !');
            exit;
        };

        echo Resp::retSuc(1, (object)array());
    }

    public function actionDelete() {
        // http://192.168.3.69/project/delete

        $projectId = Check::checkGetParam(0, 'projectId', false);

        $TblProjectFile = new TblProjectFile();
        $condArr = array(
            'condition' => 'project_id=:projectId',
            'params' => array('projectId' => $projectId)
        );
        if(!$TblProjectFile->deleteRecordAll($condArr)) {
            echo Resp::retFail(0, 'delete project files failed');
            exit;
        }

        $userId = Yii::app()->session['userId'];
        $TblProject = new TblProject();
        $condArr = array(
            'condition' => 'id=:projectId AND user_id=:userId',
            'params' => array('projectId' => $projectId, 'userId' => $userId)
        );
        if(!$TblProject->deleteRecord($condArr)) {
            echo Resp::retFail(0, 'delete project failed');
            exit;
        }

        $this->redirect(URL_PROJECT_LIST);
    }

}
